# -*- codeing = utf-8 -*-
# !/usr/bin/env python
# @coding  : utf-8
# @File    : demo1.py
# @Time    : 2022/4/18 9:48
# @Author  : xdd
# @Software: PyCharm
# @desc :$分析一个观影预测 img.png


from  sklearn.feature_extraction import DictVectorizer
from  sklearn import preprocessing
from sklearn import  tree
import csv


file_data=open('test.csv','rt')
reader=csv.reader(file_data)
# 表头信息
headers=next(reader)
print("[MYOUT]  headers:  "+str(headers))

feature_list=[]#特征集合
result_list=[] #电影是否被看
# ===============处理数据集合==========================
for row in reader:
    result_list.append(row[-1])
    #去掉首位两列,特征集中只保留'type ' , 'country ' , 'gross'
    feature_list.append(dict(zip(headers[1:-1],row[1:-1])))
print("[MYOUT]  result_list:  "+str(result_list))
print("[MYOUT]  feature_list:  "+str(feature_list))

vec = DictVectorizer()
# 将dict类型的list数据,转换成numpy array
dummyx = vec.fit_transform(feature_list).toarray() #扁平化处理
dummyY = preprocessing.LabelBinarizer().fit_transform(result_list)
#注意,dummgx是按首字母排序的'country 4' ,' gross 3 ', 'type 2'
print("[MYOUT]  dummyx:  "+str(dummyx))
print("[MYOUT]  dummyY:  "+str(dummyY))

clf=tree.DecisionTreeClassifier(criterion='entropy',random_state=0)
clf=clf.fit(dummyx,dummyY)
# print("[MYOUT]  clf:  "+str(clf))
print(clf)
#
# ================输出图形=========================
import pydotplus
dot_data=tree.export_graphviz(clf,
                              feature_names=vec.get_feature_names(),
                              filled=True,
                              rounded=True,
                              special_characters=True,
                              out_file=None
                              )
graph = pydotplus.graph_from_dot_data(dot_data)
# graph.write_pdf('film.pdf')

# ==================预测=======================
A = ([[0,0,0,1,0,1,0,1,0]])#日本(4)-低票房(2)-动画片(3))
# B = ([[0,0,1,0,0,1,0,1,0]])#法国(4)-低票房(2)-动画片(3)
# C =([[1,0,0,0,1,0,1,0,0]])#美国(4)-高票房(2)-动作片(3)#
predict_result=clf.predict(A)
print("[MYOUT]  predict_result:  "+str(predict_result))